package com.atguigu.classroom.activity.service.impl;

import com.atguigu.classroom.model.activity.CouponUse;
import com.atguigu.classroom.activity.mapper.CouponUseMapper;
import com.atguigu.classroom.activity.service.CouponUseService;
import com.atguigu.classroom.model.user.UserInfo;
import com.atguigu.classroom.userClient.UserInfoFeignClient;
import com.atguigu.classroom.vo.activity.CouponUseQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券领用表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-07-17
 */
@Service
public class CouponUseServiceImpl extends ServiceImpl<CouponUseMapper, CouponUse> implements CouponUseService {
    @Autowired
    private CouponUseMapper couponUseMapper;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;//引用远程调用接口
    @Override
    public Page<CouponUse> getPage(Long page, Long limit, CouponUseQueryVo couponUseQueryVo) {
        Page<CouponUse> couponUsePage=new Page<>(page,limit);
        LambdaQueryWrapper<CouponUse> couponUseQueryWrapper=new LambdaQueryWrapper<>();
        couponUseQueryWrapper.eq(couponUseQueryVo.getCouponId()!=null,CouponUse::getCouponId,couponUseQueryVo.getCouponId())
                .eq(couponUseQueryVo.getCouponStatus()!=null,CouponUse::getCouponStatus,couponUseQueryVo.getCouponStatus())
                .ge(couponUseQueryVo.getGetTimeBegin()!=null,CouponUse::getGetTime,couponUseQueryVo.getGetTimeBegin())
                .le(couponUseQueryVo.getGetTimeEnd()!=null,CouponUse::getGetTime,couponUseQueryVo.getGetTimeEnd());
        couponUseMapper.selectPage(couponUsePage,couponUseQueryWrapper);//sql-1：查询已使用的优惠卷的基本信息
        if(couponUsePage.getRecords().size()==0){
            return couponUsePage;
        }
        List<Long> userIds=couponUsePage.getRecords().stream().map(couponUse -> {
            Long userId = couponUse.getUserId();
            return userId;
        }).collect(Collectors.toList());
        List<UserInfo> userInfoList = userInfoFeignClient.getByIds(userIds);//sql-2:远程调用OpenFeign查询用户信息
        Map<Long, UserInfo> userInfoMap=new HashMap<>(userInfoList.size());
        for(UserInfo userInfo:userInfoList){
            userInfoMap.put(userInfo.getId(),userInfo);
        }
        List<CouponUse> couponUseList=couponUsePage.getRecords().stream().map(couponUse -> {
            CouponUse newCouponUse=new CouponUse();
            BeanUtils.copyProperties(couponUse,newCouponUse);
            newCouponUse.getParam().put("nickName",userInfoMap.get(couponUse.getUserId()).getNickName());
            newCouponUse.getParam().put("phone",userInfoMap.get(couponUse.getUserId()).getPhone());
            return newCouponUse;
        }).collect(Collectors.toList());
        couponUsePage.setRecords(couponUseList);
        return couponUsePage;
    }

    @Override
    public Boolean updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUse couponUse=new CouponUse();
        couponUse.setId(couponUseId);
        couponUse.setOrderId(orderId);
        couponUse.setCouponStatus("1");
        couponUse.setUsingTime(new Date());
        int i = couponUseMapper.updateById(couponUse);
        if(i!=0){return true;}
        return false;
    }
}
